<?php
namespace wxpay;

require_once "WxPayConfig.php";

class wxRefund
{


    function refund($out_refund_no,$transaction_id,$total_fee,$refund_fee){
        //支付信息
        //dump($out_refund_no.md5(time()));
        $config = new WxPayConfig();
        $wxchat['appid'] = $config->GetAppId();
        $wxchat['mchid'] = $config->GetMerchantId();
        $webdata = array(
            'appid'=> $wxchat['appid'],
            'mch_id'=> $wxchat['mchid'],
            'nonce_str'=> md5(time()),
            'out_refund_no'=> $out_refund_no.md5(time()),
            'transaction_id'=> $transaction_id,//微信订单号
            'total_fee'=> $total_fee*100,
            'refund_fee'=> $refund_fee*100,

        );
        foreach ($webdata as $k => $v) {
            $tarr[] =$k.'='.$v;
        }
        sort($tarr);
        $sign = implode($tarr, '&');
        $sign .= '&key='.$config->GetKey();
        $webdata['sign']=strtoupper(md5($sign));
        $wget = $this->ArrToXml($webdata);//数组转XML
        $pay_url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';//api地址
        $res = $this->postData($pay_url,$wget);//发送数据

        //dump($res);

        if(!$res){
            return array('status'=>1, 'msg'=>"Can't connect the server" );
        }

        $content = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);

         //dump($content);

        $rdata = array(
            'result_code'  => strval($content->result_code),
            'result_msg' => strval($content->result_msg)
        );
        return $rdata;
    }


    //数组转XML
    function ArrToXml($arr)
    {
        if(!is_array($arr) || count($arr) == 0) return '';
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }


//发送数据
    function postData($url,$postfields){
        $ch = curl_init();
        $params[CURLOPT_URL] = $url; //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $postfields;
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        //以下是证书相关代码
        $params[CURLOPT_SSLCERTTYPE] = 'PEM';
        $params[CURLOPT_SSLCERT] = '/www/wwwroot/yangkids/cert/apiclient_cert.pem';//绝对路径
        $params[CURLOPT_SSLKEYTYPE] = 'PEM';
        $params[CURLOPT_SSLKEY] = '/www/wwwroot/yangkids/cert/apiclient_key.pem';//绝对路径
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }





    /*
     * 获取当前服务器的IP
     * */
    public function get_client_ip(){
        if ($_SERVER['REMOTE_ADDR']) {
            $cip = $_SERVER['REMOTE_ADDR'];
        } elseif (getenv("REMOTE_ADDR")) {
            $cip = getenv("REMOTE_ADDR");
        } elseif (getenv("HTTP_CLIENT_IP")) {
            $cip = getenv("HTTP_CLIENT_IP");
        } else {
            $cip = "unknown";
        }
        return $cip;
    }



}